package com.asia.algorithmcode.backTracking;

import java.util.ArrayList;
import java.util.List;

/**
 * @DESCRIPTION: 89, 格雷编码
 * @USER: wanfu
 * @DATE: 2025/4/18 星期五 14:09
 */
public class GrayCode {

    public static void main(String[] args) {
        List<Integer> integers = new GrayCode().grayCode(4);
        integers.forEach(o -> System.out.printf(o  + "\t"));
    }

    public List<Integer> grayCode(int n) {
        List<Integer> res = new ArrayList<Integer>() {{
            add(0);
        }};
        int head = 1;
        for (int i = 0; i < n; i++) {
            for (int j = res.size() - 1; j >= 0; j--) res.add(head + res.get(j));
            head <<= 1;
        }
        return res;
    }


}
